package com.example.demo.zaqizabatest;

import org.springframework.util.Assert;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName Test
 * @Author l42142
 * @Date 2021/3/3 10:54
 * @Description TODO
 * @Version 1.0
 **/
public class Test {

    public static Boolean validatePassword1(String password) {
        // 同步cmp 1106的密码规则
        String loginUser = "admin";
        if (password.contains(loginUser)) {
            return false;
        }
        if ((password.length() < 8) || (password.length() > 32)) {
            return false;
        }
        int count = 0;
        if (password.matches(".*\\d+.*")) {
            count++;
        }
        if (password.matches(".*[a-zA-Z]+.*")) {
            count++;
        }
        if (password.matches(".*[!@$%^_\\-=+\\[\\]{}:,.?/]+.*")) {
            count++;
        }
        if ((password.matches("[a-zA-Z0-9-!@$%^_\\-=+\\[\\]{}:,.?/]{8,32}$"))){
            count++;
        }
        if (count > 3) {
            return true;
        }
        return false;
    }

    static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= (1 << 30)) ? (1 << 30) : n + 1;
    }

    public static int getNextPowerOf2(int n) {
        // 当n为非正数时，返回1，即最小的二次幂
        if (n <= 0) {
            return 1;
        }
        // 当n为2的幂次方时，返回n本身
        if ((n & (n - 1)) == 0) {
            return n;
        }
        // 其他情况，将n的二进制表示中，最高位的1后面的所有位都置为1，并加1，得到最小的二次幂
        int result = 1;
        while (result < n) {
            result <<= 1;
        }
        return result;
    }


    public static void main(String[] args) throws Exception {
        System.out.println(tableSizeFor(10));

//        HashMap<Date, String> test = new HashMap<>();
//        Date date1 = new Date();
//        long time = date1.getTime();
//        Date date2 = new Date(time);
//
//        test.put(date1, "test");
//        System.out.println(test.get(date2));
//




//        int  b =1;
//        Object a = (Object) b;
//        Assert.notNull("", "Target must not be null");
//        Assert.hasLength("a", "aaaaaaaaa");
//        Calendar calendar = Calendar.getInstance();
//        calendar.set(2021, 11, 31);
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        @SuppressWarnings("AlibabaUseRightCaseForDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
//        System.out.println(simpleDateFormat.format(calendar.getTime()));
//        System.out.println(sdf.format(calendar.getTime()));
//
//            System.out.println(com.example.demo.utils.CipherUtils.aesDncode("juOp*k24","0wYeglsNzZpEf6uIhMk0aA=="));
//
//        System.out.println(com.example.demo.utils.CipherUtils.aesEncode("juOp*k24","Ef+mG84A"));

//        ConcurrentHashMap<String, String> map1 = new ConcurrentHashMap<>();
//        map1.put("1" ,"1");
//        map1.put("11" ,"1");
//        map1.put("111" ,"1");
//        map1.clear();
//        map1.put("1" ,"1");
//        System.out.println(map1.size());
//
//        Boolean validatePassword1 = validatePassword1(",}1o0^5m");
//        System.out.println(validatePassword1);
//
//        String aesDncode = com.example.demo.utils.CipherUtils.aesDncode("juOp*k24", "ufhBHaehH7WKH0Nz4vvoqA==");
//
//        boolean matches = "012345678901234567890123456789011".matches("[a-zA-Z0-9-!@$%^_\\-=+\\[\\]{}:,.?/]{8,32}$");
//        System.out.println(matches);
//        Boolean aBoolean = validatePassword("!_aaaaaa");
//        System.out.println(aBoolean);
//        boolean f = true;
//            for (int i =0; i<100; i++){
//                randomPassword();
//
//                String password = randomPassword();
//                System.out.println(password);
//                Boolean validatePassword = validatePassword(password);
//                System.out.println(validatePassword);
//            }
//        System.out.println("12-3".matches(".*[!@$%^_+\\-={}\\[\\]:,.\\/\\?]+.*"));
//        AtomicInteger atomicInteger = new AtomicInteger(0);
//
//        for (;;){
//
//            int i = atomicInteger.get();
//
//
//            boolean b = atomicInteger.compareAndSet(i, 10);
//
//            if(b){
//                break;
//            }
//        }
//        System.out.println(atomicInteger.get());
//
//        LinkedTransferQueue linkefdTransferQueue = new LinkedTransferQueue<Thread>();
//
//        AtomicStampedReference<String> atomicStampedReference = new AtomicStampedReference(1, 1);
//        atomicStampedReference.compareAndSet("", "", 1, 1);

        /*long start = System.currentTimeMillis();
        System.out.println("test start = " + start);
        new Thread(() -> test2()).start();
        // abs为绝对值
        int abs = Math.abs(12);
        long end = System.currentTimeMillis();
        System.out.println("test end = " + end);
        System.out.println("test = " + (end - start));*/
//        Thread t1 = new Thread();
//        Thread t2 = new Thread();
//        t1.start();
//        t1.join(); // 等t1执行完再往下执行
//        t2.start();
//        t2.join(); // 在虚拟机执行中，这句可能被忽略


////        ArrayList<Integer> array = new ArrayList<>();
////        array.add(1);
////        array.add(2);
////        array.add(2);
////        array.add(3);
////        for (Integer a : array) {
////            if (2 == a){
////                array.remove(a);
////            }
////            System.out.println(a);
////        }
//
//
//        List<String> strings = new ArrayList<>();
//        strings.add("a");strings.add("b");strings.add("c");
////        for (int i = 0; i < strings.size(); i++){
////            if ("a".equals(strings.get(i))){
////                strings.remove("a");
////            }
//////            System.out.println(strings.get(i));
////        }
//
//        Iterator iterator = strings.iterator();
//        while (iterator.hasNext()){
//            if ("a".equals(iterator.next())){
//                strings.remove("a");
//            }
//        }

//        for (String s:strings){
//            if ("a".equals(s)){
//                strings.remove("a");
//            }
//        }

    }


    /**
     * 实现Cloneable接口并重写Object类中的clone()方法
     * 实现Serializable接口，通过对象的序列化和反序列化实现克隆，可以实现真正的深度克隆
     * 通过org.apache.commons中的工具类BeanUtils和PropertyUtils进行对象复制
     */
    public static void test2() {
        long start = System.currentTimeMillis();
        System.out.println("test2 start = " + start);
        int sum = 0;
        for (int i = 0; i < 1000; i++) {
            sum = sum + i;
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("test2 end = " + end);
        System.out.println("test2 = " + (end - start));
    }

    private static String createRandomPassword() {
        String uppercaseLetter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String lowercaseLetter = "abcdefghijklmnopqrstuvwxyz";
        /*String special_char = "~`!@#$%^&*()_+-={}|[]:\";'<>,./\\";*/
        String specialChar = "!@#$&*_";
        String digital = "0123456789";

        StringBuffer password = new StringBuffer();
        Random rand = new Random();
        char data = 'a';

        for (int i = 0; i < 8; i++) {
            switch (rand.nextInt(4)) {
                case 0:
                    data = uppercaseLetter.charAt(rand.nextInt(uppercaseLetter.length()));
                    password.append(data);
                    break;
                case 1:
                    data = lowercaseLetter.charAt(rand.nextInt(lowercaseLetter.length()));
                    password.append(data);
                    break;
                case 2:
                    data = specialChar.charAt(rand.nextInt(specialChar.length()));
                    password.append(data);
                    break;
                case 3:
                    data = digital.charAt(rand.nextInt(digital.length()));
                    password.append(data);
                    break;
                default:
                    break;
            }
        }
        return password.toString();
    }

    public static String randomPassword() {
        String password = createRandomPassword();
        boolean flag = true;
        while (true){
            Boolean validatePassword = false;
            try {
                validatePassword = validatePassword(password);
            } catch (Exception e){
                e.printStackTrace();
            }
            if (validatePassword) {
                System.out.println("密码为：" + password);
                return password;
            }
            password = createRandomPassword();
        }
//        for (int i = 0; i < 10; i++) {
//            Boolean validatePassword = validatePassword(password);
//            if (validatePassword) {
//                break;
//            }
//            password = createRandomPassword();
//        }
    }

    public static Boolean validatePassword(String password) throws Exception {

        if ((password.length() < 8) || (password.length() > 20)) {
            throw new Exception("密码长度必须在8-20位");
        }
        int count = 0;
        if (password.matches(".*\\d+.*")) {
            count++;
        }
        if (password.matches(".*[a-z]+.*")) {
            count++;
        }
        if (password.matches(".*[A-Z]+.*")) {
            count++;
        }
        if (password.matches(".*[~`!@#$%^&*()_+-={}|\\[\\]:\";'<>,.\\/]+.*")) {//!@#$&*_
            count++;
        }
        if (count > 2) {
            return true;
        }
        throw new Exception("密码规则不符合要求!");
    }


}
